package com.duke.shaking;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.androidquery.util.AQUtility;
import com.duke.shaking.net.AsyncHttpResponseWithResultHandler;
import com.duke.shaking.pojo.User;
import com.duke.shaking.services.PopupUpdateAlertService;
import com.loopj.android.http.AsyncHttpClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

public class BaseActivity extends Activity {
	
	public static float density = 0;
	public static float screenWith = 0;
	public static float screenHeight = 0;
	
	public static final String TAG = "shaking";
	public static final String USER_IN_BUNDLE = "user";
	
	protected static User me = null;
	//protected List<AsyncHttpClient> asyncClients;
	protected ProgressDialog progressDialog;
	protected AsyncHttpClient progressClient;
	protected AsyncHttpResponseWithResultHandler progressResponseHandler;
	
	public static User getMe() {
		return me;
	}
	
	protected static void setMe(User me) {
		BaseActivity.me = me;
	}
	
	public static boolean hasLogin() {
		if (me != null) {
			return me.hasLogin();
		}
		return false;
	}
	
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		if (density == 0) {
			DisplayMetrics dm = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(dm);
			density = dm.density;
		}
	}
	
	protected void onCancelProgressDialog() {
		// TODO Auto-generated method stub
		if (progressClient != null) {
			progressClient.cancelRequests(this, true);
			progressClient = null;
			progressResponseHandler = null;
		}
	}

	public void showProgress(String msg, boolean cancelable, 
			AsyncHttpClient client,
			AsyncHttpResponseWithResultHandler responseHandler) {
		
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(this);
			progressDialog.setCanceledOnTouchOutside(false);
			progressDialog.setOnCancelListener(new OnCancelListener() {

				public void onCancel(DialogInterface dialog) {
					// TODO Auto-generated method stub
					onCancelProgressDialog();
				}
				
			});
		}
		
		if (!progressDialog.isShowing()){
			progressClient = client;
			progressResponseHandler = responseHandler;
			if (progressResponseHandler != null) {
				progressResponseHandler.setProgressDialog(progressDialog);
			}
			progressDialog.setTitle(null);
			progressDialog.setMessage(msg);
			progressDialog.setCancelable(cancelable);
			progressDialog.show();
		}
	}
	
	public void showProgress(String msg, 
			AsyncHttpClient client, 
			AsyncHttpResponseWithResultHandler responseHandler) {
		showProgress(msg, true, client, responseHandler);
	}
	
	public void showProgress(int msgId, 
			AsyncHttpClient client, 
			AsyncHttpResponseWithResultHandler responseHandler) {
		String msg = null;
		if (msgId > 0) {
			msg = getString(msgId);
		}
		showProgress(msg, client, responseHandler);
	}
	
	public void showProgress(String msg) {
		showProgress(msg, false, null, null);
	}
	
	public void showProgress(int msgId) {
		showProgress(msgId, null, null);
	}
	
	public void showProgress() {
		showProgress(null, false, null, null);
	}
	
	public void dismissProgress() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
			if (progressClient != null) {
				progressClient.cancelRequests(this, true);
				progressClient = null;
				progressResponseHandler = null;
			}
		}
	}
	
	@Override
	protected void onStop() {
		dismissProgress();
		super.onStop();
	}
	
	public void info(String msg) {
		Log.i(TAG, msg);
	}
	
	public void debug(String msg) {
		Log.d(TAG, msg);
	}
	
	public void error(String msg) {
		Log.e(TAG, msg);
	}
	
	public void toast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}
	
	public void toast(int msgId) {
		toast(getResources().getString(msgId));
	}
	
	public void go(Class<?> cls, String extraName, Serializable extraData) {
		Intent i = new Intent(this, cls);
		if (extraName != null && extraData != null) {
			i.putExtra(extraName, extraData);
		}
    	startActivity(i);
	}
	
	public void go(Class<?> cls) {
		go(cls, null, null);
	}
	
	public static int dip2px(float dpValue) {
		final float scale = density;//getResources().getDisplayMetrics().density;
		if (scale == 0) {
			return (int)(dpValue + 0.5f);
		}
		return (int) (dpValue * scale + 0.5f);
	}

	public static int px2dip(float pxValue) {
		final float scale = density;//getResources().getDisplayMetrics().density;
		if (scale == 0) {
			return (int)(pxValue + 0.5f);
		}
		return (int) (pxValue / scale + 0.5f);
	}
/*
	public static int px2dip(float pxValue, Context context) {
		
		final float scale = density;//context.getResources().getDisplayMetrics().density;
		if (scale == 0) {
			return (int)(pxValue + 0.5f);
		}
		return (int) (pxValue / scale + 0.5f);
	}
*/
	@Override
	protected void onRestart() {
		super.onRestart();
		info("activity restart");
		
		if(me != null && me.hasLogin()) {
			//Intent intent = new Intent(this, PopupUpdateAlertService.class);
			//intent.putExtra("user", me);
			//startService(intent);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		if(me != null) {
			outState.putSerializable(USER_IN_BUNDLE, me);
		}
		
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
		
		me = (User)savedInstanceState.getSerializable(USER_IN_BUNDLE);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		//clean the file cache when root activity exit
		if(isTaskRoot()){

			//clean the file cache with advance option
			long triggerSize = 3000000; //starts cleaning when cache size is larger than 3M
			long targetSize = 2000000;      //remove the least recently used files until cache size is less than 2M
			AQUtility.cleanCacheAsync(this, triggerSize, targetSize);
		}
	}

	
}
